﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebEntities;
using com.subb.WebInfo;
using WebUI.ActionFilters;
using com.subb.Tools.Pager;

namespace WebUI.Controllers
{
    public class NewsInfoController : Controller
    {
        private const int RowCount = 30;

        public NewsInfoController()
            : base()
        {
            ViewBag.ActMenu = "newsinfo";
        }

        public ActionResult Index(int id = 1)
        {
            int cataId = Extensions.CommonFn.GetContentID("NewsInfoCataId");
            ViewBag.CataID = cataId;
            int page = id < 1 ? 1 : id;
            int start = (page - 1) * RowCount;
            WI_NewsDAL ndal = new WI_NewsDAL();
            int ttlCount;
            List<WI_News> newsList = ndal.GetAllNews(cataId, start, RowCount, out ttlCount, string.Empty);
            PagedList<WI_News> pglist = newsList.ToPagedList(page, RowCount, ttlCount);
            return View(pglist);
        }

        public ActionResult Industry(int id = 1)
        {
            int cataId = Extensions.CommonFn.GetContentID("IndustryCataId");
            ViewBag.CataID = cataId;
            int page = id < 1 ? 1 : id;
            int start = (page - 1) * RowCount;
            WI_NewsDAL ndal = new WI_NewsDAL();
            int ttlCount;
            List<WI_News> newsList = ndal.GetAllNews(cataId, start, RowCount, out ttlCount, string.Empty);
            PagedList<WI_News> pglist = newsList.ToPagedList(page, RowCount, ttlCount);
            return View(pglist);
        }

        [NewsViewCount]
        public ActionResult Details(int id)
        {
            WI_NewsDAL ndal = new WI_NewsDAL();
            WI_News newsinfo = ndal.GetNews(id, true);
            ViewBag.CataID = newsinfo.CataID ?? 0;
            return View(newsinfo);
        }

        public ActionResult LeftMenu(int cataId)
        {
            ViewBag.CataID = cataId;
            return PartialView();
        }

        public ActionResult SearchNews(string seakey, int id = 1)
        {
            int page = id < 1 ? 1 : id;
            int start = (page - 1) * RowCount;
            //string decSearch = HttpUtility.UrlDecode(seakey.Trim());
            ViewBag.SearchKey = seakey;
            WI_NewsDAL ndal = new WI_NewsDAL();
            int ttlCount;
            List<WI_News> newsList = ndal.GetAllNews(start, RowCount, out ttlCount, seakey);
            PagedList<WI_News> pglist = newsList.ToPagedList(page, RowCount, ttlCount);
            ViewBag.SearchCount = ttlCount;
            return View(pglist);
        }

        [HttpPost]
        public ActionResult SearchNews(string seakey)
        {
            //string encSearch = HttpUtility.UrlEncode(seakey.Trim());
            return RedirectToAction("SearchNews", new { seakey = seakey });
        }
    }
}
